﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Nature
{
    public class IndexBuilder<TValue>
    {
        #region Private Fields
        private Dictionary<TValue, int> m_indexLookup;
        private List<TValue> m_values = new List<TValue>();
        #endregion

        public IndexBuilder()
        {
            m_indexLookup = new Dictionary<TValue, int>();
        }

        public IndexBuilder(EqualityComparer<TValue> comparer)
        {
            m_indexLookup = new Dictionary<TValue, int>(comparer);
        }

        public int Register(TValue value)
        {
            int index = m_indexLookup.Count;
            if (m_indexLookup.ContainsKey(value)) { return m_indexLookup[value]; }
            m_indexLookup.Add(value, index);
            m_values.Add(value);
            return index;
        }

        public TValue[] GetValues() { return m_values.ToArray(); }        
    }
}
